<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Login
 *
 * @author Maciej
 */
class LoginController extends Controller {

    public $layout = '//layouts/login';

    public function beforeAction($pagename) {

        Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl . '/themes/js/custom/loginController.js', CClientScript::POS_END);
        Yii::app()->clientScript->registerCssFile(Yii::app()->baseUrl . '/themes/css/custom/loginController.css');
        return true;
    }

    public function actionIndex() {
        $this->metaKeywords = 'Panel logowania';
        $this->metaDescription = 'Panel logowania';
        $this->pageTitle = 'Panel logowania';
        
        $model = new LoginForm;
        $form = new CForm('application.views.forms.loginForm', $model);
        
        if (isset($_POST['LoginForm'])) {
            $model->attributes = $_POST['LoginForm'];
            $valid = $model->validate();
            if ($valid && $model->login()) {    
                
                if(Yii::app()->user->roles == 'admin'){
                    $this->redirect(array('/admin'));
                }else{
                    $this->redirect(array('/mod'));
                }
            }
        }
       
        $this->render('index', array('form' => $form));
    }
    
    
    public function actionLogout() {
        Yii::app()->user->logout();
        $this->redirect(array('/home'));
    }

}
